<?php
require_once(DIR_LIBRARY."orm".DS."orm.php");
class Admin_Acl_AdminUser_Index_Model extends NICED_Model
{

    public function getList($page, $page_size, $params)
    {
        $orm   = ORMFactory::get("acl.acl_adminuser");
        $rows = $orm->getList($page, $page_size, $count, $max_page, $params, "user_id");

        $this->set("count", $count);
        $this->set("max_page", $max_page);
        $this->set("page", $page);
        $this->set("adminusers", $rows);
    }

    public function getByUserId($user_id)
    {
        if (!$user_id)
        {
            throw new NICED_Exception("please assign user id");
        }

        $orm = ORMFactory::get("acl.acl_adminuser");
        $orm->setField("user_id", $user_id);
        $orm->load();
        
        if (!$orm->getLoaded())
        {
            throw new NICED_Exception("get adminuser fail");
        }

        $this->set("adminuser", $orm);
    }

    public function create($user_id, $password, $role_ids)
    {
        $orm = ORMFactory::get("acl.acl_adminuser");
        $orm->setField("user_id", $user_id);
        $orm->setPassword($password);
        $orm->setField("role_list", $role_ids);
        $this->set("adminuser", $orm);

        $bool = $orm->create();
        
        if (!$bool)
        {
            throw new NICED_Exception("create fail");
        }
        
        return $bool;
    }

    public function update($user_id, $password, $role_ids)
    {
        if (!$user_id)
        {
            throw new NICED_Exception("no user id");
        }

        $orm = ORMFactory::get("acl.acl_adminuser");
        $orm->appendWhere('user_id = "'. $user_id.'"');
        $orm->setPassword($password);
        $orm->setField("role_list", $role_ids);
        $this->set("adminuser", $orm);

        $bool = $orm->update();

        if (!$bool)
        {
            throw new NICED_Exception("update fail");
        }

        return $bool;
    }

    public function delete($user_id)
    {
        if (!$user_id)
        {
            throw new NICED_Exception("please assign user id");
        }

        require_once(DIR_LIBRARY."orm".DS."orm.php");
        $orm = ORMFactory::get("acl.acl_adminuser");
        $orm->setField("user_id", $user_id);
        $bool = $orm->delete();
        
        if (!$bool)
        {
            throw new NICED_Exception("delete fail");
        }

        return $bool;
    }

}//end class
